package com.sgb.zhaocai.view.demand.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.OfferFragmentBinding;
import com.sgb.zhaocai.entity.OfferListEntity;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.view.demand.adapter.OfferListAdapter;
import com.sgb.zhaocai.utils.PickUtil;
import com.sgb.zhaocai.viewmodel.OfferListFragmentModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

public class OfferListFragment extends AppLazyFragment {

    private static String NO = "no";
    private String no = "";
    private OfferListFragmentModel mModel;
    private OfferFragmentBinding mBinding;
    private OfferListAdapter offerlistadapter;
    private RequestBean requestBean;
    private boolean isLoadMore = false;
    private String SearchName;

    public static OfferListFragment newInstance(String no) {
        OfferListFragment fragment = new OfferListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(NO, no);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.offer_fragment, container, false);
        no = (String) getArguments().get(NO);
        mModel = new ViewModelProvider(this).get(OfferListFragmentModel.class);
        requestBean = new RequestBean();
        return mBinding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
        initDataList();
    }

    private void initDataList() {
        if (requestBean == null) {
            requestBean = new RequestBean();
        }
        if (!TextUtils.isEmpty(SearchName)) {
            requestBean.setKeyword(SearchName + "");
        }else {
            requestBean.setKeyword("");
        }
        isLoadMore = false;
        requestBean.page = 1;
        requestBean.setType(no);
        mModel.RecruitList(this, requestBean);
    }

    @Override
    protected void initView() {
        super.initView();
        if (offerlistadapter == null) {
            offerlistadapter = new OfferListAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBinding.recyclerView.setLayoutManager(layoutManager);
        mBinding.recyclerView.setHasFixedSize(true);
        //解决数据加载完成后, 没有停留在顶部的问题
        mBinding.recyclerView.setFocusable(false);
        mBinding.recyclerView.setAdapter(offerlistadapter);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initDataList();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                requestBean.page++;
                //上拉加载更多
                mModel.RecruitList(OfferListFragment.this, requestBean);

            }
        });
        offerlistadapter.setOnItemStateOperate(new OfferListAdapter.OnItemStateOperate() {
            @Override
            public void offers(OfferListEntity offerListEntity) {
                //报价
                PickUtil.startDemandOffer(getActivity(),offerListEntity.getGoodsId(),String.valueOf(offerListEntity.getType()),"","1");
            }

            @Override
            public void details(OfferListEntity offerListEntity) {
                //详情
                mModel.getWebH5(OfferListFragment.this, offerListEntity.getGoodsId(),offerListEntity.getType());
            }
        });
    }

    public void setRecruitList(List<OfferListEntity> data, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                offerlistadapter.addDatas(data);
            }
            mBinding.recyclerView.setNoMore(isLastPage);
        } else {
            if (data != null && data.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                offerlistadapter.setDatas(data);
                mBinding.recyclerView.scrollToPosition(0);
                mBinding.recyclerView.setNoMore(isLastPage);
            } else {
                offerlistadapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
            }
        }
    }
    private void RecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    public void initializePop(String name) {
        SearchName = name;
        initDataList();
    }


}
